import random

def flag(info, begin):
    if (info[begin - 1][3] - info[begin - 1][0]) / info[begin - 1][0] >= 0.005:
        return 1
    else:
        return 0

def bias(info, begin, n):
    avg_close = 0.0
    for j in range(n):
        avg_close = avg_close + info[begin + j][3]
    avg_close = avg_close / n
    result = (info[i][3] - avg_close) / avg_close
    return result

def ma(info, begin, n):
    result = 0.0
    for j in range(n):
        result = result + info[begin + j][3]
    result = result / n
    return result

def rsi(info, begin, n):
    avg_up = 0.0
    avg_down = 0.0
    for j in range(9):
        d = info[i + j][3] - info[i + j][0]
        if d > 0:
            avg_up = avg_up + d
        else:
            avg_down = avg_down - d
    return avg_up / (avg_up + avg_down)

def ema(info, begin, n):
    m = 0
    sum = 0.0
    for i in range(1, n+1):
        m = m + i
    for i in range(n):
        sum = sum + float(n - i) / m * info[begin + i][3]
    return sum    

def macd(info, begin, n1, n2, m):
    diff = 0.0
    for i in range(m):
        eman1 = ema(info, begin + m, n1)
        eman2 = ema(info, begin + m, n2)
        diff = diff + (eman1 - eman2)
    dea = diff / m
    return diff - dea
 
def kdj(info, begin, n):
    max_price = 0.0
    min_price = 100000.0
    for j in range(9):
        if raw_info[begin + j][1] > max_price:
            max_price = raw_info[begin + j][1]
        if raw_info[begin + j][2] < min_price:
            min_price = raw_info[begin + j][2]
    return (raw_info[begin][3] - min_price) / (max_price - min_price)

def skdj(info, begin, n, m):
    result = 0.0
    for j in range(m):
        result = result + kdj(info, begin, n)
    return result / m

def qstick(info, begin, n):
    result = 0.0
    for j in range(n):
        result = result + (info[begin + j][3] - info[begin + j][0])
    return result / n

if __name__ == "__main__":
    code = "HIG"
    filename1 = code + ".raw"
    f1 = open(filename1, "r")
    
    filename2 = code
    f2 = open(filename2, "w")
    
    filename3 = "predict"
    f3 = open(filename3, "a")
    
    filename4 = "ju"
    f4 = open(filename4, "a")
    
    filename5 = "sample"
    f5 = open(filename5, "a")
    
    raw_info = []        
    for line in f1:
        s = line.rstrip("\n").split(",")
        t = []
        if int(s[5]) > 10:
            for i in range(1, 6):
                t.append(float(s[i]))
            raw_info.append(t)
    
    print len(raw_info)
    
    result = []
    for i in range(1, len(raw_info) - 50):
        t = []
        
        f = flag(raw_info, i)
        t.append(f)
        
        bias12 = bias(raw_info, i, 12)
        t.append(bias12)
        
        ma10 = ma(raw_info, i, 10)
        t.append(ma10)

        skdj93 = skdj(raw_info, i, 9, 3)
        t.append(skdj93)
        #print skdj93

        rsi9 = rsi(raw_info, i, 9)
        t.append(rsi9)
        
        macd12269 = macd(raw_info, i, 12, 26, 9)
        t.append(macd12269)
        
        q8 = qstick(raw_info, i, 8)
        t.append(q8)
        #print q8
        
        result.append(t)
        #f2.write("{0} 1:{1} 2:{2} 3:{3} 4:{4} 5:{5} 6:{6}".format(t[0], t[1], t[2], t[3], t[4], t[5], t[6]))
        #f2.write("\n")
    
    sample = []
    predict = []
    up_sample = 50
    down_sample = 50
    
    up_predict = 10
    down_predict = 10
    for line in result:
        if line[0] == 1:
            if up_predict > 0:
                up_predict = up_predict - 1
                predict.append(line)
            elif up_sample > 0:
                up_sample = up_sample - 1
                sample.append(line)
        elif line[0] == 0:
            if down_predict > 0:
                down_predict = down_predict - 1
                predict.append(line)
            elif down_sample > 0:
                down_sample = down_sample - 1
                sample.append(line)
        
                
    for line in sample:
        f5.write("{0} 1:{1} 2:{2} 3:{3} 4:{4} 5:{5} 6:{6}\n".format(line[0], line[1], line[2], line[3], line[4], line[5], line[6]))
        
##    predict = []
##    for i in range(50):
##        r = random.randint(0, 2049 - i)
##        predict.append(result[r])
##        del result[r]
        
    for line in predict:
        f4.write("{0}\n".format(line[0]))
        f3.write("{0} 1:{1} 2:{2} 3:{3} 4:{4} 5:{5} 6:{6}\n".format(line[0], line[1], line[2], line[3], line[4], line[5], line[6]))
        
##    for line in result:
##        f2.write("{0} 1:{1} 2:{2} 3:{3} 4:{4} 5:{5} 6:{6}\n".format(line[0], line[1], line[2], line[3], line[4], line[5], line[6]))
    
    #print predict    
    f1.close()
    f2.close()
    f3.close()
    f4.close()
    f5.close()